home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / RgnMaster 1.0 / Source / Utils / QDUtils.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  4.4 KB  |  138 lines  |  [TEXT/KAHL]

  1. #include "QDUtils.h"
  2.  
  3. void CenterRect(Rect *innerRect, Rect *outerRect) {
  4.     short hDiff = ((outerRect->right - outerRect->left) - (innerRect->right - innerRect->left)) / 2;
  5.     short vDiff = ((outerRect->bottom - outerRect->top) - (innerRect->bottom - innerRect->top)) / 2;
  6.     innerRect->left = outerRect->left + hDiff;
  7.     innerRect->right = outerRect->right - hDiff;
  8.     innerRect->top = outerRect->top + vDiff;
  9.     innerRect->bottom = outerRect->bottom - vDiff;
  10. } // END CenterRect
  11.  
  12. void MoveRectTo(short h, short v, Rect *theRect) {
  13.     register short width = theRect->right - theRect->left;
  14.     register short height = theRect->bottom - theRect->top;
  15.     theRect->left = h;
  16.     theRect->top = v;
  17.     theRect->right = theRect->left + width;
  18.     theRect->bottom = theRect->top + height;
  19. } // END OffsetRectTo
  20.  
  21. void MoveRgnTo(short hLoc, short vLoc, RgnHandle theRgn) {
  22.     register short hDiff;
  23.     register short vDiff;
  24.     hDiff = hLoc - (**theRgn).rgnBBox.left;
  25.     vDiff = vLoc - (**theRgn).rgnBBox.top;
  26.      OffsetRgn(theRgn, hDiff, vDiff);
  27. } // END MoveRgnTo
  28.  
  29. void MoveRgnToRect(Rect *theRect, RgnHandle theRgn) {
  30.     register short hDiff;
  31.     register short vDiff;
  32.     vDiff = theRect->top - (**theRgn).rgnBBox.top;
  33.     hDiff  = theRect->left - (**theRgn).rgnBBox.left;
  34.     OffsetRgn(theRgn, hDiff, vDiff);
  35. } // END MoveRgnToRect
  36.  
  37. /*************************************************************************
  38. **************************************************************************
  39.  
  40. This Code was written by
  41.  
  42.         Brad Anderson
  43. in the Think C 6.0 environment
  44.         
  45. where ever you see
  46. "
  47.     asm {
  48.         dc.b    "QDOffscreens Copyright 1993 S-FX"
  49.     }
  50. "
  51. you are not aloud to erase this or comment this out.  If you do, the you are
  52. liable for the max. penalty in law.  Just kidding.
  53. Conditions:
  54.     1)If you do use this source code then Please leave this note in, just to
  55.       acknowledge me.
  56.     2)Please put your "About Box" that I did this.
  57.     3)If you are any way grateful for me to do this, then please, send me one
  58.       copy of the finish product.
  59.     4)No copy and pasting of this source code.  Leave it whole. You can adjust it
  60.       to work on your compiler and you can add your own error calls where necessary.
  61.     5)If you alter the source and plan to redistribute the source with stuff that you
  62.       did then contact me before hand.
  63.  
  64. Include:
  65.     MacHeaders
  66.     MacTraps    //for CGrafPort Making & BitMap making
  67.     
  68.     Feel free to give this out to anyone in its unaltered state.
  69.     My AOL Address is:
  70.         KEA6820
  71.     and CompuServe ID is:
  72.         71170,3562
  73.  
  74. **************************************************************************
  75. *************************************************************************/
  76.  
  77.  
  78. /************************************************************************
  79.  *    MakeBitMap
  80.  *        Makes a BitMap and returns it in a GrafPtr.  This is a universal way of making
  81.  *        a BitMap.  
  82.  *
  83. ************************************************************************/
  84.  
  85. Boolean NewOffScreenBitMap(GrafPtr *newOffscreen, Rect *inBounds, int id)
  86. {
  87.     GrafPtr savePort;
  88.     GrafPtr newPort;
  89.     PicHandle    pict;
  90.     
  91.     if(id != 0)
  92.         pict = GetPicture(id);        /* Get Pict    */
  93.     GetPort(&savePort);                /* Save old Port */
  94.  
  95.     newPort = (GrafPtr) NewPtr(sizeof(GrafPort));    /* makes space in RAM */
  96.     if (MemError() != noErr)                        /* if memory is low    */
  97.     {
  98.         return false;
  99.     }
  100.     OpenPort(newPort);                        /* Inits some vars    */
  101.     newPort->portRect = *inBounds;            /* set rect of port*/
  102.     newPort->portBits.bounds = *inBounds;    /* set rect of BitMap*/
  103.     RectRgn(newPort->clipRgn, inBounds);
  104.     RectRgn(newPort->visRgn, inBounds);
  105.     
  106.     /* calc. the size of each horizontal line and move it up till its / by 4 */
  107.     newPort->portBits.rowBytes = ((inBounds->right - inBounds->left + 15) >> 4) << 1;
  108.  
  109.     
  110.     newPort->portBits.baseAddr =             /* make new Ptr */
  111.         NewPtr(newPort->portBits.rowBytes * (long) (inBounds->bottom - inBounds->top));
  112.     if (MemError() != noErr)                /* if memory is low */
  113.     {
  114.         SetPort(savePort);
  115.         ClosePort(newPort);                  /* dump the visRgn and clipRgn */
  116.         DisposPtr((Ptr)newPort);             /* dump the GrafPort */
  117.         return false;                        /* tell caller we failed */
  118.     }
  119.     SetPort(newPort);                         /* Set Port before drawing */
  120.     if(id != 0)
  121.         DrawPicture(pict, inBounds);        /* Draw */
  122.     else
  123.         EraseRect(inBounds);
  124.     *newOffscreen = newPort;
  125.     SetPort(savePort);                        /* Restore Old Port */
  126.     return true;
  127. }
  128.  
  129.  
  130. /*********** DestroyOffscreen ***********/
  131. /* Destroys a BitMap */
  132.  
  133. void DestroyOffscreenBitMap(GrafPtr oldOffscreen)
  134. {
  135.     ClosePort(oldOffscreen);
  136.     DisposPtr(oldOffscreen->portBits.baseAddr);
  137.     DisposPtr((Ptr)oldOffscreen);
  138. }